package com.example.paopao.consumer.memer.service.impl;

import com.example.paopao.common.pojo.security.CurrenPrincipal;
import com.example.paopao.common.pojo.temp.RacheInfo;
import com.example.paopao.consumer.memer.dao.cache.IIntegralsCacheRepository;
import com.example.paopao.consumer.memer.dao.persist.repository.IIntegralRecheRepository;
import com.example.paopao.consumer.memer.pojo.dto.IntegraRacheDTO;
import com.example.paopao.consumer.memer.pojo.vo.IntegraRacheVO;
import com.example.paopao.consumer.memer.service.IIntegraRacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class IntegralRacheServiceImpl implements IIntegraRacheService {

    @Autowired
    private IIntegralRecheRepository integralRecheRepository;

    @Autowired
    private IIntegralsCacheRepository integralsCacheRepository;

    @Override
    public IntegraRacheVO resultIntegralR(IntegraRacheDTO integraRacheDTO, CurrenPrincipal principal) {

        long timestamp=System.currentTimeMillis();
        /*用户名+时间毫秒级为订单号*/
        String orderNum=principal.getUserId().toString()+timestamp;

        /*返回给前端的数据*/
        IntegraRacheVO integraRacheVO=new IntegraRacheVO();
        integraRacheVO.setOutTradeNo(orderNum);
        String integraName ="充值"+ integraRacheDTO.getTransAmount() +"获取"+integraRacheDTO.getIntegra().toString();

        integraRacheVO.setSubject(integraName);
        integraRacheVO.setTotalAmount(integraRacheDTO.getTransAmount());

        /*将数据放到redis*/
        RacheInfo racheInfo=new RacheInfo();
        racheInfo.setUserId(principal.getUserId());
        racheInfo.setUsername(principal.getUsername());
        racheInfo.setSubject(integraRacheVO.getSubject());
        racheInfo.setIntegral(integraRacheDTO.getIntegra());
        racheInfo.setOutTradeNo(integraRacheVO.getOutTradeNo());
        racheInfo.setTotalAmount(integraRacheVO.getTotalAmount());
        integralsCacheRepository.putOrder(racheInfo);
        return integraRacheVO;
    }
}
